package com.etbsms.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import com.etbsms.dao.IRoleDAO;
import com.etbsms.domain.Role;

@Repository
@Component("roleDAO")
public class RoleDAOImpl extends GenericDAOImpl<Role, String> 
	implements IRoleDAO {

	@Override
	public boolean save(Role entity) {
		boolean result = false;
		Session session = sessionFactory.openSession();		
		try{
			Transaction tx = session.beginTransaction();
			session.save(entity);			
			tx.commit();		
			result = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		session.close();
		return result;
	}
	
	@Override
	public boolean update(Role entity) {
		boolean result = false;
		Session session = sessionFactory.openSession();		
		try{
			Transaction tx = session.beginTransaction();
			session.update(entity);			
			tx.commit();		
			result = true;
		}catch(Exception e){
			e.printStackTrace();
		}
		session.close();
		return result;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Role> getAll() {
		Session session = sessionFactory.openSession();
		Criteria criteria = session.createCriteria(Role.class);
		criteria.add(Restrictions.eq("enabled",true));
		criteria.addOrder(Order.asc("sort"));
		List<Role> list = criteria.list();
		session.close();
		return list;
	}
}
